package com.cn.bukeManage.service.serviceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.cn.bukeManage.dao.CouponDao;
import com.cn.bukeManage.pojo.Coupons;
import com.cn.bukeManage.pojo.page.CouponsPage;
import com.cn.bukeManage.pojo.page.Page;
import com.cn.bukeManage.service.CouponService;
@Service("couponService")
public class CouponServiceImpl implements CouponService {
	@Resource
	private CouponDao couponDao;
	//分页显示所有的优惠卷
	public List<Map<String,Object>> getAllCouponsSet(Model model,Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		List<Coupons> list=couponDao.getAllCoupons(page);
		Integer rows=couponDao.getAllRowsOfCoupons();
		page.setRows(rows);
		model.addAttribute("page", page);
		List<Map<String,Object>> list2=new ArrayList<>();
		for(Coupons coupons:list){
			Map<String,Object> map=new HashMap<>();
			map.put("couponsId", coupons.getCouponsId());
			map.put("knock", coupons.getKnock());
			map.put("fullRedultion", coupons.getFullRedultion());
			map.put("beginTime", coupons.getBeginTime());
			map.put("failureTime", coupons.getFailureTime());
			map.put("beginString", coupons.getBeginString());
			map.put("overString", coupons.getOverString());
			map.put("forWho", coupons.getForWho());
			list2.add(map);
		}
		return list2;
	}
	//增加新的优惠卷
	public Integer addNewCoupons(Integer knock,Integer fullRedultion,String beginString,String overString,String beginTime,String failureTime,Integer forWho) {
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		Coupons coupons=new Coupons();
		coupons.setKnock(knock);
		coupons.setFullRedultion(fullRedultion);
		coupons.setBeginString(beginString);
		coupons.setOverString(overString);
		coupons.setForWho(forWho);		
		try {
			coupons.setBeginTime(simpleDateFormat.parse(beginTime));
			coupons.setFailureTime(simpleDateFormat.parse(failureTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Integer i=couponDao.addNewCoupon(coupons);
		return i;
	}
	//修改优惠卷
	public Integer updateCoupons(Integer couponsId,Integer knock,Integer fullRedultion,String beginString,String overString,String beginTime,String failureTime,Integer forWho) {
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

		Coupons coupons=new Coupons();
		coupons.setCouponsId(couponsId);
		coupons.setKnock(knock);
		coupons.setFullRedultion(fullRedultion);
		coupons.setBeginString(beginString);
		coupons.setOverString(overString);
		coupons.setForWho(forWho);
		try {
			coupons.setBeginTime(simpleDateFormat.parse(beginTime));
			coupons.setFailureTime(simpleDateFormat.parse(failureTime));
		} catch (ParseException e) {
	
			e.printStackTrace();
		}
		Integer integer=couponDao.updateCoupons(coupons);
		return integer;
	}
	//根据不同的类型得到不同的卷
	public List<Map<String, Object>> getAllCouponsByForWho(Model model, Integer forWho,Integer currentPage) {
		CouponsPage page=new CouponsPage();
		page.setCurrentPage(currentPage);
		page.setForWho(forWho);
		List<Coupons> list=couponDao.getAllCouponsByForWho(page);
		Integer rows=couponDao.getAllRowsOfCouponsByForWho(page);
		page.setRows(rows);
		model.addAttribute("page", page);
		List<Map<String,Object>> list2=new ArrayList<>();
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		for(Coupons coupons:list){
			Map<String,Object> map=new HashMap<>();
			map.put("couponsId", coupons.getCouponsId());
			map.put("knock", coupons.getKnock());
			map.put("fullRedultion", coupons.getFullRedultion());
			map.put("beginTime", simpleDateFormat.format(coupons.getBeginTime()));
			map.put("failureTime", simpleDateFormat.format(coupons.getFailureTime()));
			map.put("beginString", coupons.getBeginString());
			map.put("overString", coupons.getOverString());
			map.put("forWho",coupons.getForWho());
			list2.add(map);
		}
		return list2;
	}
	//根据优惠卷id获取他的所有信息
	public Map<String, Object> getCouponsById(Integer couponsId) {
		Coupons coupons=couponDao.getCouponsById(couponsId);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		Map<String,Object> map=new HashMap<>();
		map.put("couponsId",coupons.getCouponsId());
		map.put("knock",coupons.getKnock());
		map.put("fullRedultion",coupons.getFullRedultion());
		map.put("beginTime",simpleDateFormat.format(coupons.getBeginTime()));
		map.put("failureTime",simpleDateFormat.format(coupons.getFailureTime()));
		map.put("beginString",coupons.getBeginString());
		map.put("overString",coupons.getOverString());
		map.put("forWho",coupons.getForWho());
		return map;
	}
	
}
